package utils.test;
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.text.ParseException;
import javax.swing.*;
import javax.swing.text.*;

public class TstJFormattedTextFieldListening
{
    public static void main( String[] args )
    {
        final JLabel actionLabel = new JLabel( "", JLabel.CENTER );

        actionLabel.setBorder( 
                BorderFactory.createTitledBorder( "action" ) 
                );

        final JLabel propertyLabel = new JLabel( "", JLabel.CENTER );

        propertyLabel.setBorder( 
                BorderFactory.createTitledBorder( "property change" ) 
                );

        final JLabel focusLabel = new JLabel( "", JLabel.CENTER );

        focusLabel.setBorder( BorderFactory.createTitledBorder( "focus" ) );

        MaskFormatter mask = null;

        try {
            mask = new MaskFormatter( "####" );
        }
        catch( ParseException pe ) {
            System.out.println( "mask parse: " + pe.getMessage() );
        }

        final JFormattedTextField ftf = new JFormattedTextField( mask );

        Dimension d = ftf.getPreferredSize();

        ftf.setPreferredSize( new Dimension( 70, d.height ) );
        ftf.setHorizontalAlignment( JTextField.CENTER );
        ftf.addActionListener( 
                new ActionListener()
                {
                    public void actionPerformed( ActionEvent e )
                    {
                        String value = (String)ftf.getValue();

                        actionLabel.setText( value );
                    }
                });
        ftf.addPropertyChangeListener( "value", 
                new PropertyChangeListener()
                {
                    public void propertyChange( PropertyChangeEvent e )
                    {
                        String newValue = (String)e.getNewValue();

                        propertyLabel.setText( newValue );
                    }
                });
        ftf.addFocusListener( 
                new FocusAdapter()
                {
                    public void focusLost( FocusEvent e )
                    {
                        try {
                            ftf.commitEdit();
                        }
                        catch( ParseException pe ) {
                            System.out.println( "focus commit parse: "
                            + pe.getMessage() );
                        }

                        String value = (String)ftf.getValue();

                        focusLabel.setText( value );
                    }
                });

        JPanel north = new JPanel();

        north.add( ftf );

        JPanel panel = new JPanel( new GridLayout( 0, 1 ) );

        panel.add( actionLabel );
        panel.add( propertyLabel );
        panel.add( focusLabel );

        JTextField tf = new JTextField( 12 );

        JPanel south = new JPanel();

        south.add( tf );

        JFrame f = new JFrame();

        f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        f.getContentPane().add( north, "North" );
        f.getContentPane().add( panel );
        f.getContentPane().add( south, "South" );
        f.setSize( 300, 255 );
        f.setLocation( 200, 200 );
        f.setVisible( true );
    }
}